<?php

namespace App\Services\AetherUpload\Lib;

use App\Services\AetherUpload\Eloquent\DriverResource;
use App\Services\AliOss\AliOssAdapter;
use App\Services\AliOss\AliOssAdapterTrait;

class OssResource extends DriverResource
{
    use AliOssAdapterTrait;

    private $adapter;

    public function __construct($path, $disk)
    {
        parent::__construct($path, $disk);

    }

    public function append($path, $data, $position = 0)
    {
        // TODO: Implement append() method.
        return $this->getAdapter()->appendStream($path, $data, $position);
    }

    public function calculateHash()
    {
        try {
            $hash = md5_file($this->getAdapter()->getUrl($this->path));
        } catch (\Exception $exception) {
            $hash = null;
        }
        return $hash;
    }

    private function getAdapter(): AliOssAdapter
    {
        if (!$this->adapter) {
            $this->adapter = $this->getOssAdapter($this->config);
        }
        return $this->adapter;
    }


}
